home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / backtrace / Point.h < prev    next >
C/C++ Source or Header  |  1996-11-11  |  9KB  |  365 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #ifndef POINT_H
  38. #define POINT_H
  39.  
  40. #ifndef POINT_EXTERN
  41. #define POINT_EXTERN extern
  42. #endif 
  43.  
  44. const float point_fudge = .000001;
  45.  
  46. class Point {
  47.   public:
  48.     inline Point operator=(Point a);
  49.     inline Point operator=(GLfloat *a);
  50.     inline Point operator+(Point a);
  51.     inline Point operator+=(Point a);
  52.     inline Point operator-(Point a);
  53.     // This takes a cross-product
  54.     inline Point operator*(Point b);
  55.     inline Point operator*(GLfloat b);
  56.     inline Point operator/(GLfloat b);
  57.     inline Point operator/=(GLfloat b);
  58.     inline GLfloat& operator[](int index);
  59.  
  60.     inline GLfloat dist(Point b);
  61.     inline GLfloat dot(Point b);
  62.     inline GLfloat mag();
  63.     inline GLfloat magsquared();
  64.     inline Point unit();
  65.     inline void unitize();
  66.  
  67.     // Angle is in RADIANS
  68.     inline Point rotate(Point axis, GLfloat angle);
  69.     inline Point rotate(Point axis, GLfloat c, GLfloat s);
  70.     inline void rotate_self(Point axis, GLfloat c, GLfloat s);
  71.     Point rotate_abouty(GLfloat c, GLfloat s);
  72.  
  73.     // Returns point projected through proj_pt into XY plane
  74.     // Does nothing if proj_pt - *this is parallel to XY plane
  75.     inline Point project(Point proj_pt);
  76.     inline void project_self(Point proj_pt);
  77.     inline void Point::project_self(GLfloat px, GLfloat py, GLfloat pz);
  78.     inline Point project_direction(Point direction);
  79.     inline Point Point::project_direction(GLfloat x, GLfloat y, GLfloat z);
  80.     // This projects (px, py, pz) into this in direction (dx, dy, dz)
  81.     inline void Point::compute_projected(GLfloat px, GLfloat py, GLfloat pz,
  82.                      GLfloat x, GLfloat y, GLfloat z);
  83.  
  84.     // Returns point projected through light and refracted into XY
  85.     // plane.  
  86.     // N is normal at point (ie normal at *this)
  87.     // I is the index of refraction
  88.     inline Point refract(Point light, Point N, GLfloat I);
  89.     void refract_self(Point light, Point N, GLfloat I);
  90.     Point refract_direction(Point light, Point N, GLfloat I);
  91.  
  92.     inline void glvertex();
  93.     inline void glnormal();
  94.  
  95.     void print();
  96.     void print(const char *format);
  97.  
  98.     GLfloat pt[4];
  99.   private:
  100. };
  101.  
  102. POINT_EXTERN Point val;
  103.  
  104. #define DOT(a, b) (a.pt[0]*b.pt[0] + a.pt[1]*b.pt[1] + a.pt[2]*b.pt[2])
  105. #define THIS_DOT(b) (pt[0]*b.pt[0] + pt[1]*b.pt[1] + pt[2]*b.pt[2])
  106.  
  107. inline Point Point::operator=(Point a)
  108. {
  109.   pt[0] = a.pt[0];
  110.   pt[1] = a.pt[1];
  111.   pt[2] = a.pt[2];
  112.   pt[3] = a.pt[3];
  113.   return *this;
  114. }
  115.  
  116. inline Point Point::operator=(GLfloat *a)
  117. {
  118.   pt[0] = a[0]; 
  119.   pt[1] = a[1]; 
  120.   pt[2] = a[2]; 
  121.   pt[3] = 1;
  122.   return *this;
  123. }
  124.  
  125. inline Point Point::operator+(Point a)
  126. {  
  127.   val.pt[0] = pt[0] + a.pt[0];
  128.   val.pt[1] = pt[1] + a.pt[1];
  129.   val.pt[2] = pt[2] + a.pt[2]; 
  130.   return val; 
  131. }
  132.  
  133. inline Point Point::operator+=(Point a)
  134. {
  135.   pt[0] += a.pt[0];
  136.   pt[1] += a.pt[1];
  137.   pt[2] += a.pt[2];
  138.   return *this;
  139. }
  140.  
  141. inline Point Point::operator-(Point a)
  142. {
  143.   val.pt[0] = pt[0] - a.pt[0];
  144.   val.pt[1] = pt[1] - a.pt[1];
  145.   val.pt[2] = pt[2] - a.pt[2];
  146.   return val;
  147. }
  148.   
  149. inline Point Point::operator*(Point b)
  150. {
  151.   val.pt[0] = pt[1]*b.pt[2] - b.pt[1]*pt[2];
  152.   val.pt[1] = pt[2]*b.pt[0] - b.pt[2]*pt[0];
  153.   val.pt[2] = pt[0]*b.pt[1] - pt[1]*b.pt[0];
  154.   return val;
  155. }
  156.  
  157. inline Point Point::operator*(GLfloat b)
  158. {
  159.   val.pt[0] = pt[0] * b;
  160.   val.pt[1] = pt[1] * b;
  161.   val.pt[2] = pt[2] * b;
  162.   return val;
  163. }
  164.  
  165. inline Point Point::operator/(GLfloat b)
  166. {
  167.   val.pt[0] = pt[0] / b;
  168.   val.pt[1] = pt[1] / b;
  169.   val.pt[2] = pt[2] / b;
  170.   return val;
  171. }
  172.  
  173. inline Point Point::operator/=(GLfloat b)
  174. {
  175.   pt[0] /= b;
  176.   pt[1] /= b;
  177.   pt[2] /= b;
  178.   return *this;
  179. }
  180.  
  181. inline GLfloat& Point::operator[](int index)
  182. {
  183.   return pt[index];
  184. }
  185.  
  186. inline GLfloat Point::dist(Point b)
  187. {
  188.   return (*this - b).mag();
  189. }
  190.  
  191. inline GLfloat Point::dot(Point b)
  192. {
  193.   return pt[0]*b.pt[0] + pt[1]*b.pt[1] + pt[2]*b.pt[2];
  194. }
  195.  
  196. inline GLfloat Point::mag()
  197. {
  198.   return sqrt(pt[0]*pt[0] + pt[1]*pt[1] + 
  199.               pt[2]*pt[2]);
  200. }
  201.  
  202. inline GLfloat Point::magsquared()
  203. {
  204.   return pt[0]*pt[0] + pt[1]*pt[1] + pt[2]*pt[2];
  205. }
  206.  
  207. inline Point Point::unit()
  208. {
  209.   GLfloat m = sqrt(pt[0]*pt[0] + pt[1]*pt[1] + pt[2]*pt[2]);
  210.   val.pt[0] = pt[0] / m;
  211.   val.pt[1] = pt[1] / m;
  212.   val.pt[2] = pt[2] / m;
  213.   return val;
  214. }
  215.  
  216. inline void Point::unitize()
  217. {
  218.   GLfloat m = sqrt(pt[0]*pt[0] + pt[1]*pt[1] + pt[2]*pt[2]);
  219.   pt[0] /= m;
  220.   pt[1] /= m;
  221.   pt[2] /= m;
  222. }
  223.  
  224. inline Point Point::rotate(Point axis, GLfloat angle)
  225. {
  226.   return rotate(axis, cos(angle), sin(angle));
  227. }
  228.  
  229. inline Point Point::rotate(Point axis, GLfloat c, GLfloat s)
  230. {
  231.   float x = axis.pt[0], y = axis.pt[1], z = axis.pt[2], t = 1.0 - c;
  232.   float tx, ty;
  233.   
  234.   tx = t*x;
  235.   /* Taking advantage of inside info that this is a common case */
  236.   if (y == 0.0) {
  237.     val.pt[0] = pt[0]*(tx*x + c) + pt[1]*(-s*z) + pt[2]*(tx*z);
  238.     val.pt[1] = pt[0]*(s*z) + pt[1]*c + pt[2]*(-s*x);
  239.     val.pt[2] = pt[0]*(tx*z) + pt[1]*s*x + pt[2]*(t*z*z + c);
  240.   } else {
  241.     ty = t*y;
  242.     val.pt[0] = pt[0]*(tx*x + c) + pt[1]*(tx*y - s*z) +
  243.       pt[2]*(tx*z + s*y);
  244.     val.pt[1] = pt[0]*(tx*y + s*z) + pt[1]*(ty*y + c) +
  245.       pt[2]*(ty*z - s*x);
  246.     val.pt[2] = pt[0]*(tx*z - s*y) + pt[1]*(ty*z + s*x) +
  247.       pt[2]*(t*z*z + c);
  248.   }
  249.   return val;
  250. }
  251.  
  252. inline void Point::rotate_self(Point axis, GLfloat c, GLfloat s)
  253. {
  254.   float Px, Py, Pz;
  255.   float x = axis.pt[0], y = axis.pt[1], z = axis.pt[2], t = 1.0 - c;
  256.   float tx, ty;
  257.   
  258.   tx = t*x;
  259.   Px = pt[0];
  260.   Py = pt[1];
  261.   Pz = pt[2];
  262.   /* Taking advantage of inside info that this is a common case */
  263.   if (!y) {
  264.     pt[0] = Px*(tx*x + c) +    Py*(-s*z) +     Pz*(tx*z);
  265.     pt[1] = Px*(s*z) +         Py*c +         Pz*(-s*x);
  266.     pt[2] = Px*(tx*z) +     Py*s*x +     Pz*(t*z*z + c);
  267.   } else {
  268.     ty = t*y;
  269.     pt[0] = Px*(tx*x + c) +     Py*(tx*y - s*z) +
  270.       Pz*(tx*z + s*y);
  271.     pt[1] = Px*(tx*y + s*z) +     Py*(ty*y + c) +
  272.       Pz*(ty*z - s*x);
  273.     pt[2] = Px*(tx*z - s*y) +     Py*(ty*z + s*x) +
  274.       Pz*(t*z*z + c);
  275.   }
  276. }  
  277.  
  278. inline void Point::glvertex() 
  279. {
  280.   glVertex3fv(pt);
  281. }
  282.  
  283. inline void Point::glnormal()
  284. {
  285.   glNormal3fv(pt);
  286. }
  287.  
  288. inline Point Point::project(Point proj_pt)
  289. {
  290.   GLfloat dirx = pt[0] - proj_pt.pt[0], 
  291.       diry = pt[1] - proj_pt.pt[1],
  292.       dirz = pt[2] - proj_pt.pt[2];
  293.   GLfloat t;
  294.  
  295.   if (fabs(dirz) < point_fudge) val = *this;
  296.   else {
  297.     t = -proj_pt.pt[2] / dirz;
  298.     val.pt[0] = proj_pt.pt[0] + dirx*t;
  299.     val.pt[1] = proj_pt.pt[1] + diry*t;
  300.     val.pt[2] = 0.0;
  301.   }
  302.   return val;
  303. }
  304.  
  305. // This naively assumes that proj_pt[z] != this->pt[z]
  306. inline void Point::project_self(Point proj_pt)
  307. {
  308.   GLfloat dirx = pt[0] - proj_pt.pt[0], 
  309.       diry = pt[1] - proj_pt.pt[1],
  310.       dirz = pt[2] - proj_pt.pt[2];
  311.   GLfloat t;
  312.  
  313.   t = -proj_pt.pt[2] / dirz;
  314.   pt[0] = proj_pt.pt[0] + dirx*t;
  315.   pt[1] = proj_pt.pt[1] + diry*t;
  316.   pt[2] = 0.0;
  317. }
  318.  
  319. inline void Point::project_self(GLfloat px, GLfloat py, GLfloat pz)
  320. {
  321.   GLfloat dirx = pt[0] - px, 
  322.   diry = pt[1] - py,
  323.   dirz = pt[2] - pz, t;
  324.  
  325.   t = -pz / dirz;
  326.   pt[0] = px + dirx*t;
  327.   pt[1] = py + diry*t;
  328.   pt[2] = 0.0;
  329. }
  330.  
  331. inline Point Point::project_direction(Point direction) {
  332.   GLfloat t;
  333.  
  334.   t = -pt[2] / direction.pt[2];
  335.   val.pt[0] = pt[0] + direction.pt[0]*t;
  336.   val.pt[1] = pt[1] + direction.pt[1]*t;
  337.   val.pt[2] = 0;
  338.   return val;
  339. }
  340.  
  341. inline Point Point::project_direction(GLfloat x, GLfloat y, GLfloat z) 
  342. {
  343.   GLfloat t;
  344.  
  345.   t = -pt[2] / z;
  346.   val.pt[0] = pt[0] + x*t;
  347.   val.pt[1] = pt[1] + y*t;
  348.   val.pt[2] = 0;
  349.   return val;
  350. }
  351.  
  352. inline void Point::compute_projected(GLfloat px, GLfloat py, GLfloat pz,
  353.                      GLfloat dx, GLfloat dy, GLfloat dz)
  354. {
  355.   GLfloat t = -pz / dz;
  356.   pt[0] = px + dx*t;
  357.   pt[1] = py + dy*t;
  358.   pt[2] = 0;
  359. }
  360.  
  361.  
  362. #undef POINT_EXTERN
  363.  
  364. #endif
  365.